//
//  AdView.m
//  FinanceNews
//
//  Created by qianfeng on 15-6-8.
//  Copyright (c) 2015年 qianfeng. All rights reserved.
//

#import "AdView.h"
#import "UIImageView+WebCache.h"
#define kScreenSize [UIScreen mainScreen].bounds.size

@interface UIScrollView ()<UIScrollViewDelegate>

@end

@implementation AdView
int numberOfImage;
-(void)setImageNames:(NSMutableArray *)imageNames{
     NSInteger j = 0 ;
    NSLog(@"%ld",imageNames.count);
    numberOfImage = (int)imageNames.count;
//    for (int i=0; i<imageNames.count; i++) {
//        
//        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i*kScreenSize.width, 0, kScreenSize.width, 150)];
//        [imageView sd_setImageWithURL:[NSURL URLWithString:imageNames[i]]];
//        [_imageArr addObject:imageView];
//        //[self addSubview:imageView];
//        
//    }
    
    for (NSInteger i = 0; i < _imageArr.count+2; i++) {
        if (i == 0) {
            j = _imageArr.count-1;
        }else if (i == _imageArr.count+1) {
            j = 0;
        }else {
            j = i - 1;
        }
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i*kScreenSize.width, 0, kScreenSize.width, 150)];
        [imageView sd_setImageWithURL:[NSURL URLWithString:imageNames[j]]];
        [self addSubview:imageView];
        
    }
    
    self.contentSize = CGSizeMake(kScreenSize.width*(imageNames.count+2), 120);

    self.contentOffset = CGPointMake(kScreenSize.width, 0);
    self.bounces = NO;
    self.pagingEnabled = YES;
    self.delegate = self;
}

//减速停止调用
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    //获取偏移量
    CGPoint curP = scrollView.contentOffset;
    if (curP.x == 0) {
        //如果x偏移量 == 0-》假的第五张
        //立即滚动到真的第五张 (不要动画)
        scrollView.contentOffset = CGPointMake(numberOfImage*scrollView.bounds.size.width, 0);
    }else if (curP.x == (numberOfImage+1)*scrollView.bounds.size.width) {
        //如果偏移量 是假的第一张
        //立即偏移
        scrollView.contentOffset = CGPointMake(1*scrollView.bounds.size.width, 0);
    }
}


@end
